package com.briup.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;

import org.apache.hadoop.mapred.lib.db.DBConfiguration;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.db.DBOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

public class StudentScore2DB extends Configured implements Tool {
    public static  class StudentScore2DBMapper extends Mapper<LongWritable, Text,Text, IntWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
//            将一行数据转换成String 并切割通过"-"
            String[] split = value.toString().split("[-]");
//            将学号 姓名  班级 拼接
            String keyOut=split[1]+","+split[2]+","+split[3];
            int valueOut=Integer.parseInt(split[4]);
            context.write(new Text(keyOut),new IntWritable(valueOut));
        }
    }

    public static  class  StudentScore2DBReducer extends Reducer<Text,IntWritable,Student, NullWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum=0;
            int count=0;
            for (IntWritable value : values) {
                sum+=value.get();
                count++;
            }
//           将key里边的学号,姓名,班级  转成String 然后切割通过,
            String[] datas = key.toString().split("[,]");
//            构建学生对象
            Student stu = new Student();
//            将学生属性值 赋值给学生对象
            stu.setId(datas[0]);
            stu.setName(datas[1]);
            stu.setClassName(datas[2]);
            stu.setSumScore(sum);
            stu.setAvgScore(sum/count);
            context.write(stu,NullWritable.get());
        }
    }
    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        DBConfiguration.configureDB(conf,"com.mysql.cj.jdbc.Driver","jdbc:mysql://server:3306/briup","root","root");
        String in = conf.get("in");
        Path path = new Path(in);

        Job job = Job.getInstance(conf);
        job.setJobName("StudentScore2DB");
        job.setJarByClass(this.getClass());

        TextInputFormat.addInputPath(job,path);
        job.setMapperClass(StudentScore2DBMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setReducerClass(StudentScore2DBReducer.class);
        job.setOutputKeyClass(Student.class);
        job.setOutputValueClass(NullWritable.class);
        job.setOutputFormatClass(DBOutputFormat.class);

        DBOutputFormat.setOutput(job,"student","id", "name","class","sum","avg");


        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        ToolRunner.run(new StudentScore2DB(),args);
    }
}
